const db=wx.cloud.database()
const shop=db.collection('shop')
const $=db.command.aggregate
Page({
    data:{
        localtionList:['所有饭堂','第一饭堂','第二饭堂','第三饭堂','第四饭堂'],
        listNum:0,
    },
    async onLoad(){
        this.getShop(0)
    },
    async select(e){//选择饭堂
        console.log(e)
        let localtion=Number(e.detail.value)
        this.getShop(localtion)
        this.setData({
            listNum:e.detail.value
        })
    },
    async getShop(localtion){//根据点赞数量排名获取店铺
        wx.showLoading({
          title: '加载中',
        })
        let res
        let local = Number(localtion)
        if(localtion==0){
            res=await shop.aggregate()
            .project({
                img:1,
                localtion:1,
                name:1,
                zan:$.size('$zan'),
                cai:$.size('$cai')
            })
            .sort({
              zan:-1
            })
            .limit(10)
            .end()
            console.log('所有',res)
        }else{
          console.log(local)
            res=await shop.aggregate()
            .match({
              localtion:local
            })
            .project({
                img:1,
                localtion:1,
                name:1,
                zan:$.size('$zan'),
                cai:$.size('$cai')
            })
            .sort({
              zan:-1
            })
            .limit(10)
            .end()
            console.log(res)
        }
        let all=res.list
        let top=all.slice(0,3)
        let list=all.slice(3,11)
        this.setData({
            top,list
        })
        wx.hideLoading()
    },
    jumpPage(e){
        console.log('jumpPage',e)
        wx.navigateTo({
          url: '/pages/orther/content?type=all&id='+e.currentTarget.dataset.id,
        })
    },
    onPullDownRefresh(){
      let that=this
      wx.stopPullDownRefresh({
        success(){
            that.getShop(that.data.listNum)
        }
      })
  }
})